﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utils
{
    public static partial class ExtensionHelper
    {
        #region Random
        /// <summary>
        /// 打乱 List集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="inputList"></param>
        /// <returns></returns>
        public static List<T> Random<T>(this List<T> inputList)
        {
            //Copy to a array
            T[] copyArray = new T[inputList.Count];
            inputList.CopyTo(copyArray);

            //Add range
            List<T> copyList = new List<T>();
            copyList.AddRange(copyArray);

            //Set outputList and random
            List<T> outputList = new List<T>();
            Random rd = new Random(DateTime.Now.Millisecond);

            while (copyList.Count > 0)
            {
                //Select an index and item
                int rdIndex = rd.Next(0, copyList.Count - 1);
                T remove = copyList[rdIndex];

                //remove it from copyList and add it to output
                copyList.Remove(remove);
                outputList.Add(remove);
            }
            return outputList;
        }
        #endregion

        #region ToList 把字符串按照指定分隔符装成 List 去除重复  

        /// <summary>  
        /// 把字符串按照指定分隔符装成 List 去除重复  
        /// </summary>  
        /// <param name="str"></param>  
        /// <param name="sepeater">分隔符</param>
        /// <param name="isRemoveRepeat">是否去除重复项 默认为flase</param>
        /// <returns></returns>  
        public static List<string> ToList(this string str, char[] sepeater, bool isRemoveRepeat = false)
        {
            var list = new List<string>();

            foreach (string item in str.Split(sepeater))
            {
                if (string.IsNullOrEmpty(item)) continue;
                if (!isRemoveRepeat)
                {
                    list.Add(item);
                }
                else if (list.Any(o => o != item.Trim()))
                {
                    list.Add(item);
                }

            }
            return list;
        }
        #endregion

        /// <summary>
        ///  ToList 把字符串按照指定分隔符装成 List 去除重复  
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="sepeater">分隔符</param>
        /// <param name="isRemoveRepeat">是否移除重复</param>
        /// <returns></returns>
        public static List<int> ToIntList(this string str, char[] sepeater, bool isRemoveRepeat = false)
        {
            var list = new List<int>();
            if (string.IsNullOrWhiteSpace(str)) return list;
            foreach (string item in str.Split(sepeater))
            {
                if (string.IsNullOrEmpty(item)) continue;
                var value = 0;
                var state = int.TryParse(item, out value);
                if (state == false) continue;//不能数字类型，跳过

                if (!isRemoveRepeat)
                {
                    list.Add(value);
                }
                else if (list.Any(o => o != value))
                {
                    list.Add(value);
                }

            }
            return list;
        }
    }
}
